﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace GEAR_Structure.ClimateData
{
    [Serializable]
    public class ClimateData
    {
        protected List<Province> provinces;

        public ClimateData() 
        {
            provinces = new List<Province>();
        }

        public List<Province> Provinces
        {
            get { return provinces; }
            set { provinces = value; }
        }

        //ritorna la provincia dal comune dato
        public Province getByCommon(Common c)
        {
            Province ret = null;
            var provincie = from p in this.provinces
                            where p.hasCommon(c)
                            select p;
            foreach (Province p in provincie)
                ret = p;
            return ret;
        }

        //Ritorna la lista dei comuni dei dati climatici
        public List<Common> commonToList()
        {
            List<Common> ret = new List<Common>();
            foreach (Province p in this.provinces)
                foreach (Common c in p.Commons)
                    ret.Add(c);
            ret = ret.OrderBy(x => x.Locality).ToList();
            return ret;
        }

        public void addProvince(Province p)
        {
            provinces.Add(p);
        }

        public void import()
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(@"C:\Users\tino.mastino\Desktop\Daticlima.xml");
            XmlNodeList listProvincie = xdoc.SelectNodes("NewDataSet/provincie");
            foreach (XmlNode provincia in listProvincie)
            {
                Province p = new Province();
                p.Abbreviation = provincia.SelectSingleNode("prov").InnerText;
                p.Name = provincia.SelectSingleNode("provincia").InnerText;
                p.Region = provincia.SelectSingleNode("reg").InnerText;
                p.GG = int.Parse(provincia.SelectSingleNode("GG").InnerText);
                p.Climate_zone = provincia.SelectSingleNode("zona").InnerText.ToUpper()[0];
                p.Project_temperature = double.Parse(provincia.SelectSingleNode("Tprog").InnerText.Replace(',','.'));
                p.Max_temperature = double.Parse(provincia.SelectSingleNode("Tmax").InnerText.Replace('.', ','));
                p.DeltaT = double.Parse(provincia.SelectSingleNode("DeltaT").InnerText.Replace('.', ','));
                p.Altitude = double.Parse(provincia.SelectSingleNode("alt").InnerText.Replace('.', ','));

                double lat = double.Parse(provincia.SelectSingleNode("latGr").InnerText.Replace('.', ','));
                lat += double.Parse(provincia.SelectSingleNode("latPr").InnerText.Replace('.', ',')) / 100;
                p.Latitude = lat;


                double lng = double.Parse(provincia.SelectSingleNode("longGr").InnerText.Replace('.', ','));
                lng += double.Parse(provincia.SelectSingleNode("longPr").InnerText.Replace('.', ',')) / 100;
                p.Longitude = lng;

                p.Average_irradiance = double.Parse(provincia.SelectSingleNode("irradianza").InnerText.Replace('.', ','));
                p.Wind_zone = double.Parse(provincia.SelectSingleNode("vento").InnerText.Replace('.', ','));

                XmlNodeList listComuni = xdoc.SelectNodes("NewDataSet/GG[prov = '"+p.Abbreviation+"']");
                foreach (XmlNode comune in listComuni)
                {
                    Common c = new Common();
                    c.Climate_zone = comune.SelectSingleNode("z").InnerText.ToUpper()[0];
                    c.GG = int.Parse(comune.SelectSingleNode("gg").InnerText);
                    c.Locality = comune.SelectSingleNode("comune").InnerText;
                    c.Altitude = double.Parse(comune.SelectSingleNode("alt").InnerText);
                    c.Altitude_min = double.Parse(comune.SelectSingleNode("altMin").InnerText);
                    c.Altitude_max = double.Parse(comune.SelectSingleNode("altMax").InnerText);

                    double lat_c = double.Parse(comune.SelectSingleNode("latGr").InnerText.Replace('.', ','));
                    lat_c += double.Parse(comune.SelectSingleNode("latPr").InnerText.Replace('.', ',')) / 100;
                    c.Latitude = lat_c;

                    double lng_c = double.Parse(comune.SelectSingleNode("longGr").InnerText.Replace('.', ','));
                    lng_c += double.Parse(comune.SelectSingleNode("longPr").InnerText.Replace('.', ',')) / 100;
                    c.Longitude = lng_c;
                    p.Commons.Add(c);
                }

                XmlNodeList listpressioni = xdoc.SelectNodes("NewDataSet/Pressioni[Sigla_x0020_Provincia = '" + p.Abbreviation + "']");
                foreach (XmlNode pressione in listpressioni)
                {
                    double[] a = new double[12];
                    a[0] = double.Parse(pressione.SelectSingleNode("Gennaio").InnerText.Replace('.', ','));
                    a[1] = double.Parse(pressione.SelectSingleNode("Febbraio").InnerText.Replace('.', ','));
                    a[2] = double.Parse(pressione.SelectSingleNode("Marzo").InnerText.Replace('.', ','));
                    a[3] = double.Parse(pressione.SelectSingleNode("Aprile").InnerText.Replace('.', ','));
                    a[4] = double.Parse(pressione.SelectSingleNode("Maggio").InnerText.Replace('.', ','));
                    a[5] = double.Parse(pressione.SelectSingleNode("Giugno").InnerText.Replace('.', ','));
                    a[6] = double.Parse(pressione.SelectSingleNode("Luglio").InnerText.Replace('.', ','));
                    a[7] = double.Parse(pressione.SelectSingleNode("Agosto").InnerText.Replace('.', ','));
                    a[8] = double.Parse(pressione.SelectSingleNode("Settembre").InnerText.Replace('.', ','));
                    a[9] = double.Parse(pressione.SelectSingleNode("Ottobre").InnerText.Replace('.', ','));
                    a[10] = double.Parse(pressione.SelectSingleNode("Novembre").InnerText.Replace('.', ','));
                    a[11] = double.Parse(pressione.SelectSingleNode("Dicembre").InnerText.Replace('.', ','));
                    p.Average_monthly_pressure = a;
                }

                XmlNodeList listTemperature = xdoc.SelectNodes("NewDataSet/Temperature[Sigla_x0020_Provincia='" + p.Abbreviation + "']");
                foreach (XmlNode temperatura in listTemperature)
                {
                    double[] a = new double[12];
                    a[0] = double.Parse(temperatura.SelectSingleNode("Gennaio").InnerText.Replace('.', ','));
                    a[1] = double.Parse(temperatura.SelectSingleNode("Febbraio").InnerText.Replace('.', ','));
                    a[2] = double.Parse(temperatura.SelectSingleNode("Marzo").InnerText.Replace('.', ','));
                    a[3] = double.Parse(temperatura.SelectSingleNode("Aprile").InnerText.Replace('.', ','));
                    a[4] = double.Parse(temperatura.SelectSingleNode("Maggio").InnerText.Replace('.', ','));
                    a[5] = double.Parse(temperatura.SelectSingleNode("Giugno").InnerText.Replace('.', ','));
                    a[6] = double.Parse(temperatura.SelectSingleNode("Luglio").InnerText.Replace('.', ','));
                    a[7] = double.Parse(temperatura.SelectSingleNode("Agosto").InnerText.Replace('.', ','));
                    a[8] = double.Parse(temperatura.SelectSingleNode("Settembre").InnerText.Replace('.', ','));
                    a[9] = double.Parse(temperatura.SelectSingleNode("Ottobre").InnerText.Replace('.', ','));
                    a[10] = double.Parse(temperatura.SelectSingleNode("Novembre").InnerText.Replace('.', ','));
                    a[11] = double.Parse(temperatura.SelectSingleNode("Dicembre").InnerText.Replace('.', ','));
                    p.Average_monthly_temperature = a;
                }
                this.provinces.Add(p);
            }
           save(@"C:\Users\tino.mastino\Desktop\exportClimateData.xml");
        }

        //Serializza i dati climatici nell'xml
        public void save(string path)
        {
            System.IO.StreamWriter stream = new System.IO.StreamWriter(path);
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(ClimateData));
            x.Serialize(stream, this);
            stream.Close();
        }

        //Deserializza i dati climatici dall'xml
        public static ClimateData load(string path)
        {
            System.IO.StreamReader stream = new System.IO.StreamReader(path);
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(ClimateData));
            ClimateData ret = (ClimateData)x.Deserialize(stream);
            stream.Close();
            return ret;
        }

        public static ClimateData load()
        {
            return load(AppDomain.CurrentDomain.BaseDirectory + @"data/ClimateData.xml");
        }
    }
}

